package com.bidevalution.controller.api;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bidevalution.business.context.SysIdentityCode;
import com.bidevalution.business.service.BeMethodStepJobService;
import com.bidevalution.business.service.BeSysIdentityService;
import com.bidevalution.controller.dto.BeMethodStepJobDTO;
import com.bidevalution.dao.entity.BeMethodStepJob;
import com.bidevalution.support.context.Constants;
import com.google.common.collect.Lists;
import com.qtp.core.model.Response;
import com.qtp.core.util.IdsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/beMethodStepJob")
@Api(tags = "环节执行任务管理")
public class BeMethodStepJobController extends BaseController{
	@Autowired
	private BeMethodStepJobService beMethodStepJobService;
	@Autowired
	private BeSysIdentityService beSysIdentityService;
	@ApiOperation(value = "根据环节ID查询环节执行任务", notes = "根据环节ID查询环节执行任务", response = BeMethodStepJob.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "stepId", value = "环节ID", required = true, dataType = "String", paramType = "query")})
	@GetMapping("/getJobList")
	public Response<List<BeMethodStepJobDTO>> selectList(@RequestParam("stepId") String stepId) {
		List<BeMethodStepJobDTO> dtoList = Lists.newArrayList();
		for(BeMethodStepJob beMethodStepJob:beMethodStepJobService.selectList(stepId)) {
			dtoList.add(copyBean(new BeMethodStepJobDTO(), beMethodStepJob));
		}
		return Response.ok(dtoList);
	}
	@PostMapping("/insertJob")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "jobName", value = "任务名称", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "jobClass", value = "任务执行类", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "jobContent", value = "执行参数值", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "stepId", value = "环节ID", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "orderNum", value = "排序", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "stage", value = "执行阶段(0:执行前,1：执行后，2：执行中)", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "jobLevel", value = "执行级别(expert:专家,step:环节)", required = true, dataType = "String", paramType = "query")})
	@ApiOperation(value = "新增任务执行类", notes = "新增任务执行类")
	public Response<String> insert(BeMethodStepJobDTO beMethodStepJobDTO){
		
		BeMethodStepJob beMethodStepJob = new BeMethodStepJob();
		beMethodStepJob.setJobClass(beMethodStepJobDTO.getJobClass());
		beMethodStepJob.setJobContent(beMethodStepJobDTO.getJobContent());
		beMethodStepJob.setJobName(beMethodStepJobDTO.getJobName());
		beMethodStepJob.setStage(beMethodStepJobDTO.getStage());
		if("3".equals(beMethodStepJobDTO.getStage())) {
			beMethodStepJob.setIsAdvanceExecute(Constants.TRUE);
		}else {
			beMethodStepJob.setIsAdvanceExecute(Constants.FALSE);
		}
		beMethodStepJob.setStepId(beMethodStepJobDTO.getStepId());
		beMethodStepJob.setCreateTime(new Date());
		beMethodStepJob.setSeqId(IdsUtil.uuid());
		beMethodStepJob.setUpdateTime(new Date());
		beMethodStepJob.setJobCount(1);
		beMethodStepJob.setUnionChildren(beMethodStepJobDTO.getUnionChildren());
		beMethodStepJob.setLayoutCode(beMethodStepJobDTO.getLayoutCode());
		beMethodStepJob.setJobCode(beSysIdentityService.getValue(SysIdentityCode.MethodStepJobCode));
		int size = beMethodStepJobService.selectList(beMethodStepJobDTO.getStepId()).size();
		beMethodStepJob.setOrderNum(size+1);
		beMethodStepJobService.insert(beMethodStepJob);
		return Response.ok();
	}
	@PostMapping("/updateJob")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "seqId", value = "主键", required = true, dataType = "String", paramType = "query")})
	@ApiOperation(value = "修改任务", notes = "修改任务")
	public Response<String> update(@RequestParam(value = "seqId",required = true)String seqId,BeMethodStepJobDTO beMethodStepJobDTO){
		BeMethodStepJob beMethodStepJob = beMethodStepJobService.selectById(seqId);
		if(beMethodStepJob != null) {
			beMethodStepJob.setJobClass(beMethodStepJobDTO.getJobClass());
			beMethodStepJob.setJobContent(beMethodStepJobDTO.getJobContent());
			beMethodStepJob.setJobName(beMethodStepJobDTO.getJobName());
			beMethodStepJob.setStage(beMethodStepJobDTO.getStage());
			beMethodStepJob.setOrderNum(Integer.valueOf(beMethodStepJobDTO.getOrderNum()));
			beMethodStepJob.setUpdateTime(new Date());
			if("3".equals(beMethodStepJobDTO.getStage())) {
				beMethodStepJob.setIsAdvanceExecute(Constants.TRUE);
			}else {
				beMethodStepJob.setIsAdvanceExecute(Constants.FALSE);
			}
			beMethodStepJob.setJobCount(1);
			beMethodStepJob.setUnionChildren(beMethodStepJobDTO.getUnionChildren());
			beMethodStepJob.setLayoutCode(beMethodStepJobDTO.getLayoutCode());
			beMethodStepJobService.update(beMethodStepJob);
			return Response.ok();
		}else {
			return Response.fail("记录不存在");
		}
	}
	@PostMapping("/deleteJob")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "seqId", value = "主键", required = true, dataType = "String", paramType = "query")})
	@ApiOperation(value = "删除任务", notes = "删除任务")
	public Response<String> delete(@RequestParam(value = "seqId",required = true)String seqId){
		beMethodStepJobService.deleteById(seqId);
		return Response.ok();
		
	}
}
